package home1;

public class Home2 {
    public static int maxarraySum(int[] nums) {
        int maxSum = nums[0];
        int currSum = nums[0];
        for (int i = 1; i < nums.length; i++) {
            currSum = Math.max(nums[i], currSum + nums[i]);
            maxSum = Math.max(maxSum, currSum);
        }

        return maxSum;
    }

    public static void main(String[] args) {
        int[] nums1 = {1, -2, 3, 5, -1};
        int[] nums2 = {1, -2, 3, -8, 5, 1};
        int[] nums3 = {1, -2, 3, -2, 5, 1};

        int result1 = maxarraySum(nums1);
        int result2 = maxarraySum(nums2);
        int result3 = maxarraySum(nums3);

        System.out.println("数组 [1, -2, 3, 5, -1] 的最大子数组和为：" + result1);
        System.out.println("数组 [1, -2, 3, -8, 5, 1] 的最大子数组和为：" + result2);
        System.out.println("数组 [1, -2, 3, -2, 5, 1] 的最大子数组和为：" + result3);
    }
}

